import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class Program1Test {

	public static String[] strings = {"one","two",
        "three","four","five","six","seven","eight",
        "nine","ten","zero","computer","mouse","screen",
        "laptop","book","decimal","binary","quake"};
    
	// StringHolder is a wrapper around my BinaryTree obj
    StringHolder sh = null, inSh = null;
	
    
    
	public static void main(String[] args) {
		Program1Test progTest = new Program1Test();
		
		// Populate the tree
		progTest.setUp();
		
		FileInputStream fis = null;
		try {
			ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream("tree-Program1.dat"));
			os.writeObject(progTest.sh);
			
			System.out.print("Tree before serialization:  ");
			progTest.sh.tree_InOrderPrint();
			System.out.print("\n");
			
			fis = new FileInputStream("tree-Program1.dat");
			ObjectInputStream is = new ObjectInputStream(fis);
			Object o = is.readObject();
			progTest.inSh = (StringHolder)o;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	
		if (progTest.sh.isEqual(progTest.inSh) == false)
			throw new RuntimeException("Serialization read failed.  In & Out serialized objects are not equal.");

		System.out.print("Tree after serialization:  ");
		progTest.inSh.tree_InOrderPrint();
		System.out.println("\n");
		
		// Print File Stats here
		File file = new File("tree-Program1.dat");
		long filesize = file.length();
        long filesizeInKB = filesize / 1024;
        System.out.println("Size of serialized file is: " + filesize + " bytes");
	}

	public void setUp(){
	        
		sh = new StringHolder();
		
		 for(int i=0;i<strings.length;i++){
	            String s = new String(strings[i]);
	            //System.out.print(" "+s);
	            sh.addString(s);
	     }

	}

}
